//var LoadingModal = $("<div id='loading' class='modal'><img src='../resources/css/image/loading.gif' /></div>");
var USER_NAME = "${USER.name}";
if(!USER_NAME) {
    window.location.href = "login.jsp";
}
//加载content区域的数据
function loadContent(uri, c, type) {
    if ("post" == type) {
        $.post(uri, c, function (result) {
            if (result.indexOf("j_spring_security_check") > 0) {
                // session过期时重新返回到登陆页面
                window.location.reload();
                return true;
            }
            if (c) {
                if (c.toString().indexOf("fieldName") > 0) {
                    $("[oper=content]").attr("history", uri + "?" + c);
                } else {
                    var history = $("[oper=content]").attr("history");
                    if (history) {
                        var huri = history.substring(0, history.indexOf("?"));
                        var hc = history.substring(history.indexOf("?") + 1);
                        loadContent(huri, hc, "post");
                        return false;
                    }
                }
            }
            $("[oper=content]").empty().html(result);
            // 日期控件
            $("[datepicker]").datepicker({
                dateFormat: "yy-mm-dd",
                monthNames: [ "- 01月", "- 02月", "- 03月", "- 04月", "- 05月", "- 06月", "- 07月", "- 08月", "- 09月", "- 10月", "- 11月", "- 12月" ],
                showMonthAfterYear: true,
                changeMonth: true,
                changeYear: true
            });
        }, "html");
    } else {
        $.get(uri, c, function (result) {
            if (result && result.indexOf("j_spring_security_check") > 0) {
                // session过期时重新返回到登陆页面
                window.location.reload();
                return true;
            }
            if (c) {
                if (c.toString().indexOf("fieldName") > 0) {
                    $("[oper=content]").attr("history", uri + "?" + c);
                } else {
                    var history = $("[oper=content]").attr("history");
                    if (history) {
                        var huri = history.substring(0, history.indexOf("?"));
                        var hc = history.substring(history.indexOf("?") + 1);
                        loadContent(huri, hc, "post");
                        return false;
                    }
                }
            }
            $("[oper=content]").empty().html(result);
            // 日期控件
            $("[datepicker]").datepicker({
                dateFormat: "yy-mm-dd",
                monthNames: [ "- 01月", "- 02月", "- 03月", "- 04月", "- 05月", "- 06月", "- 07月", "- 08月", "- 09月", "- 10月", "- 11月", "- 12月" ],
                showMonthAfterYear: true,
                changeMonth: true,
                changeYear: true
            });
        }, "html");
    }
};
$(function () {

    $(document).on("keypress", function (e) {
        if (e.keyCode == 8) {
            var tag = e.target.tagName;
            if (tag.length > 1 && "INPUT,TEXTAREA".indexOf(tag) != -1) {
                return true;
            } else {
                return false;
            }
        }

    });

    // 给地址栏中的地址加上一个“#”
    location.href = location.href + "#";
    $(document).ajaxError(function (e, xhr, opt) {
        if (xhr.status == 403) {
            // alert("Error requesting " + opt.url + ": " + xhr.status + " " + xhr.statusText);
            // return;
            alert("操作超时，请重新登陆！");
            window.location.href = "login.jsp"
            return;
        } else if (xhr.status == 404) {
            loadContent("denied.jsp");
            return;
        }
        alert(xhr.status);
    });
    // jquery ajax不缓存
    $.ajaxSetup({
        cache: false
    });
    // $("#myModal").jquerymove();
    $.post("frame/html", function (resutl) {
        $("#div").html(resutl);
    }, "html");
    $("[oper]").each(function () {
        var uri = $(this).attr("href");
        var self = $(this);
        if (uri != "") {
            $.post(uri, function (result) {
                self.html(result);
            }, "html");
        }
    });
    // oper="accordion"中的a标签点击事件
    $("[oper=accordion] a[item]").die().live("click", function () {
        var uri = $(this).attr("href");
        if (uri != "") {
            try {
                // 菜单跳转时清掉history
                $("[oper=content]").removeAttr("history");
                loadContent(uri);
            } catch (e) {
                alert("error");
            }
        }
        return false;
    });
    // content中的a标签点击事件
    $("[oper=content] a").die().live("click", function () {
        var deny = $(this).attr("deny");
        if (deny == "not") {
            return true;
        }
        var uri = $(this).attr("href");
        if (uri && uri != "") {
            if (uri.indexOf("jumpPage") > 0) {
                return true;
            } else if (uri.indexOf("sort") > 0) {
                return true;
            } else {
                var confirms = $(this).attr("confirm");
                if (confirms) {
                    if (confirm(confirms)) {
                        loadContent(uri, true);
                    }
                } else {
                    loadContent(uri);
                }
            }
        }
        return false;
    });
    // 返回
    $("[oper=content] [back]").die().live("mousedown", function () {
        if ($(this).attr("back") == "back") {
            var history = $("[oper=content]").attr("history");
            if (history) {
                var huri = history.substring(0, history.indexOf("?"));
                var hc = history.substring(history.indexOf("?") + 1);
                loadContent(huri, hc, "post");
            } else {
                var backMenu = $(".icon-arrow-right").parent().find("a");
                backMenu.click();
            }
            return false;
        }
    });
    $("[oper=content] [onclick]").die().live("mousedown", function () {
        if ($(this).attr("onclick").toString().indexOf("back") > 0) {
            var history = $("[oper=content]").attr("history");
            if (history) {
                var huri = history.substring(0, history.indexOf("?"));
                var hc = history.substring(history.indexOf("?") + 1);
                loadContent(huri, hc, "post");
            } else {
                var backMenu = $(".icon-arrow-right").parent().find("a");
                backMenu.click();
            }
            return false;
        }
    });
    // content中的form提交
    $("form").die().live("submit", function () {
        var c = $(this).serialize();
        var uri = $(this).attr("action");
        loadContent(uri, c, "post");
        return false;
    });

    $("#closeMenu").toggle(function () {
        var _self = $(this);
        $(".iaccordion").fadeOut(500, function () {
            $(".icontent").css("width", "99.5%");
            _self.text("展开菜单");
        });

    }, function () {
        $(this).text("收起菜单");
        $(".icontent").css("width", "84.5%");
        $(".iaccordion").fadeIn(500, function () {
        });
    });
});
var msgAlert = {
    modalDom: "#myModal_msg",
    msgDom: "#myModal_msg .modal_msg",
    show: function (msg) {
        $(this.msgDom).html(msg);
        $(this.modalDom).modal("show");
    },
    close: function () {
        $(this.modalDom).modal("hide");
    }

};


var _applyAngularScope = function ($rootScope, $scope) {
    if (!$rootScope.$$phase) {
        $scope.$apply();
    }
};


window.applyAngularScope = _applyAngularScope;

Array.prototype.remove = function (dx) {
    if (isNaN(dx) || dx > this.length) {
        return false;
    }
    delete this[dx];
    for (var i = 0, n = 0; i < this.length; i++) {
        if (this[i] != this[dx]) {
            this[n++] = this[i];
        }
    }
    this.length -= 1;
}